package org.ushio.speech_module.speech.dialoguebook;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.ushio.speech_module.notification.NotificationService;

import java.io.IOException;
import java.util.List;

/**
 * @author Asus
 */
@Service
public class DialogueBookService {

    private static final int MAX_TITLE_LENGTH = 100;
    private static final int MAX_DESCRIPTION_LENGTH = 500;

    @Autowired
    private DialogueBookMapper dialogueBookMapper;

    @Autowired
    private NotificationService notificationService;

    public void validateDialogueBook(DialogueBookUploadDTO uploadDTO) {
        // 检查标题
        if (uploadDTO.getTitle() == null || uploadDTO.getTitle().isEmpty()) {
            throw new IllegalArgumentException("标题不能为空");
        }
        if (uploadDTO.getTitle().length() > MAX_TITLE_LENGTH) {
            throw new IllegalArgumentException("标题过长，最大长度为 " + MAX_TITLE_LENGTH + " 字符");
        }

        // 检查描述
        if (uploadDTO.getDescription() != null && uploadDTO.getDescription().length() > MAX_DESCRIPTION_LENGTH) {
            throw new IllegalArgumentException("描述过长，最大长度为 " + MAX_DESCRIPTION_LENGTH + " 字符");
        }
    }

    // 获取书籍信息
    @Transactional(readOnly = true)
    public DialogueBook getBookById(Long bookId) {
        return dialogueBookMapper.findById(bookId);
    }

    // 获取所有书籍信息
    @Transactional(readOnly = true)
    public List<DialogueBook> getAllBooks() {
        return dialogueBookMapper.findAll();
    }

    // 添加新对话本
    @Transactional
    public void addDialogueBook(DialogueBookUploadDTO uploadDTO) throws IOException {
        // 验证输入数据
        validateDialogueBook(uploadDTO);

        // 创建 DialogueBook 实体对象并设置属性
        DialogueBook book = new DialogueBook();
        book.setTitle(uploadDTO.getTitle());
        book.setDescription(uploadDTO.getDescription());

        // 设置图片字段
        if (uploadDTO.getPicture() != null && !uploadDTO.getPicture().isEmpty()) {
            book.setPicture(uploadDTO.getPicture().getBytes());
        }

        // 插入书籍信息，book_id 由数据库自动生成
        int result = dialogueBookMapper.insert(book);
        if (result == 0) {
            throw new RuntimeException("书籍插入失败");
        } else {
            String message = "系统新增了对话本材料" + uploadDTO.getTitle() + "噢!";
            notificationService.createNotification(message);
        }
    }

    /**
     * 更新对话本信息
     */
    @Transactional
    public void updateDialogueBook(Long id, DialogueBookUploadDTO uploadDTO) throws IOException {
        DialogueBook book = new DialogueBook();
        book.setBookId(id);
        book.setTitle(uploadDTO.getTitle());
        book.setDescription(uploadDTO.getDescription());

        // 设置图片字段
        if (uploadDTO.getPicture() != null && !uploadDTO.getPicture().isEmpty()) {
            book.setPicture(uploadDTO.getPicture().getBytes());
        }

        int result = dialogueBookMapper.update(book);
        if (result == 0) {
            throw new RuntimeException("书籍更新失败");
        }
    }

    /**
     * 根据 ID 删除对话本信息
     */
    @Transactional
    public void deleteDialogueBook(Long id) {
        // 检查该对话本下是否有章节
        int chapterCount = dialogueBookMapper.countChaptersByBookId(id);
        if (chapterCount > 0) {
            // 如果有章节，抛出异常或返回错误
            throw new RuntimeException("删除失败，请先删除该对话本的所有章节！");
        }

        // 如果没有章节，删除对话本
        int result = dialogueBookMapper.deleteById(id);
        if (result == 0) {
            throw new RuntimeException("书籍删除失败, ID: " + id + " 不存在");
        }
    }

}